package com.ngovanhau.justsort.model;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

import com.ngovanhau.justsort.MainGameActivity;
import com.ngovanhau.justsort.MySprite;
import com.ngovanhau.justsort.item.config.MyConfig;

public class Background extends MySprite{
	private BitmapTextureAtlas bg_bottom_BTA;
	private Sprite bg_bottom_SP;
	private TextureRegion bg_bottom_TR;
	
	private BitmapTextureAtlas bg_top_BTA;
	private Sprite bg_top_SP;
	private TextureRegion bg_top_TR;
	
	private BitmapTextureAtlas bg_BTA;
	private Sprite bg_SP;
	private TextureRegion bg_TR;
	
	int Y_BOTTOM = 0;
	int Y_TOP = 0;
	
	public void onLoadResources(Engine enginex, Context contextx){
		this.engine = enginex;
		this.context = contextx;
		this.mainGame = (MainGameActivity) contextx;
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
		
		this.bg_top_BTA = new BitmapTextureAtlas(512, 512);
		this.bg_top_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.bg_top_BTA, contextx, "game_bg_title.png",0,0);
		engine.getTextureManager().loadTexture(this.bg_top_BTA);
		
		this.bg_BTA = new BitmapTextureAtlas(512, 1024);
		if(LevelSetting.currentLevel>=1 && LevelSetting.currentLevel<=5){
			this.bg_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.bg_BTA, contextx, "bg"+ LevelSetting.currentLevel +".png",0,0);
		}else{
			this.bg_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.bg_BTA, contextx, "game_bg.png",0,0);
		}
		engine.getTextureManager().loadTexture(this.bg_BTA);
		
		this.bg_bottom_BTA = new BitmapTextureAtlas(512, 512);
		this.bg_bottom_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.bg_bottom_BTA, contextx, "game_time.png",0,0);
		engine.getTextureManager().loadTexture(this.bg_bottom_BTA);		
	}
	
	public void onLoadScene(Scene scene){
		this.scene = scene;
		//wall
		this.bg_SP = new Sprite(0, 0, MyConfig.SCREENWIDTH, MyConfig.SCREENHEIGHT, bg_TR);
		scene.attachChild(bg_SP);
		scene.setZIndex(0);
		scene.sortChildren();
		
		//top
		Y_TOP = (int)(MyConfig.HEIGHT_TOP * MyConfig.RACE_HEIGHT);
//		float h = (int)(bg_top_TR.getHeight() * MyConfig.RACE_HEIGHT);
//		this.bg_top_SP = new Sprite(0, 0, MyConfig.SCREENWIDTH, h, bg_top_TR);
		this.bg_top_SP = new Sprite(0, 0, MyConfig.SCREENWIDTH, Y_TOP, bg_top_TR);
		scene.attachChild(bg_top_SP);
		bg_top_SP.setZIndex(99);
		scene.sortChildren();
		
		//bottom bg timer
		float hx = (int) (bg_bottom_TR.getHeight() * MyConfig.RACE_HEIGHT);
		float wx = (int) (bg_bottom_TR.getWidth() * MyConfig.RACE_WIDTH);
		Y_BOTTOM = (int)(MyConfig.SCREENHEIGHT - hx);
		this.bg_bottom_SP = new Sprite(MyConfig.SCREENWIDTH - wx, Y_BOTTOM, wx, hx, bg_bottom_TR);
		scene.attachChild(bg_bottom_SP);
		scene.setZIndex(0);
		scene.sortChildren();
	}
}
